<template>
  <el-dialog
      :modelValue="visible"
      @update:modelValue="$emit('update:visible', $event)"
      title="新建会话"
      width="400px"
  >
    <el-form>
      <el-form-item label="会话名称">
        <el-input v-model="sessionName" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="$emit('update:visible', false)">取消</el-button>
      <el-button type="primary" @click="handleConfirm">创建</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus' // 添加ElMessage导入

const props = defineProps({
  visible: Boolean
})

const emit = defineEmits(['update:visible', 'confirm'])

const sessionName = ref('')

const handleConfirm = () => {
  if (!sessionName.value.trim()) {
    ElMessage.error('会话名称不能为空') // 添加错误提示
    return
  }
  emit('confirm', sessionName.value)
  sessionName.value = '' // 清空输入
  emit('update:visible', false)
}

watch(() => props.visible, (val) => {
  if (!val) sessionName.value = ''
})
</script>